package com.smzd.framework.oauth2.actions;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smzd.framework.annotation.Access;
import com.smzd.framework.core.actions.AbstractController;
import com.smzd.framework.core.business.IFGService;
import com.smzd.framework.oauth2.business.ClientAppService;
import com.smzd.framework.oauth2.business.ResourceService;
import com.smzd.framework.oauth2.domain.ClientApp;
import com.smzd.framework.oauth2.domain.Resource;
import com.smzd.framework.security.business.AuthorityService;
import com.smzd.framework.security.domain.Authority;
import com.smzd.framework.system.domain.Role;
import com.smzd.framework.utils.JsonHelper;
import com.smzd.framework.utils.db.RPage;
import com.smzd.utils.UUIDUtil;

@Controller
@RequestMapping("/oauth/clientapps")
@Access(Role.TAG_ROLE_OAUTH2)
public class ClientAppController extends AbstractController<ClientApp> {
    @Autowired
    ClientAppService service;
    @Autowired
    ResourceService resService;
    @Autowired
    AuthorityService authService;

    @Override
    protected IFGService<ClientApp> getService() {
        return service;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    @Override
    public String toAddPage(Map<String, Object> model, ClientApp elem, BindingResult result) throws Exception {
        return super.toAddPage(model, elem, result);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    @Override
    public String postAddData(Map<String, Object> model, ClientApp elem, BindingResult result) throws Exception {
        elem.setClientId(UUIDUtil.getRandomUUID());
        elem.setClientSecret(UUIDUtil.getRandomUUID());
        return super.postAddData(model, elem, result);
    }

    @RequestMapping(method = RequestMethod.GET)
    @Override
    public String toListPage(Map<String, Object> model, ClientApp elem, BindingResult result) throws Exception {
        return super.toListPage(model, elem, result);
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    @Override
    public String getListData(Map<String, Object> model, RPage<ClientApp> page, BindingResult result) throws Exception {
        return super.getListData(model, page, result);
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    @Override
    public String toEditPage(Map<String, Object> model, ClientApp elem, BindingResult result) throws Exception {
        elem = service.getItemById(elem.getId());
        return super.toEditPage(model, elem, result);
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    @Override
    public String postEditData(Map<String, Object> model, ClientApp elem, BindingResult result) throws Exception {
        return super.postEditData(model, elem, result);
    }

    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    @ResponseBody
    @Override
    public String postRemoveData(Map<String, Object> model, ClientApp elem, BindingResult result) throws Exception {
        return super.postRemoveData(model, elem, result);
    }

    // 资源列表页面
    @RequestMapping(value = "/{clientAppId}/resources", method = RequestMethod.GET)
    public String toResourcesPage(Map<String, Object> model, @PathVariable("clientAppId") int clientAppId) {
        model.put("clientAppId", clientAppId);
        if (clientAppId > 0) {
            List<Resource> resources = resService.getResourcesByClientAppId(clientAppId);
            model.put("resources", resources);
        }
        return super.getMyView("resources.ftl");
    }

    // 编辑资源列表数据
    @RequestMapping(value = "/{clientAppId}/resources/edit", method = RequestMethod.POST)
    @ResponseBody
    public String editClientAppResources(Map<String, Object> model, @PathVariable("clientAppId") int clientAppId,
            @RequestParam("resourceIds") String resourceIds) throws Exception {
        try {
            if (StringUtils.isEmpty(resourceIds)) {
                return JsonHelper.getSuccessJsonString("没有任何变动提交！");
            }
            service.saveAppClientResources(clientAppId, resourceIds);
            return JsonHelper.getSuccessJsonString("保存成功！");
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    // 授权列表页面
    @RequestMapping(value = "/{clientAppId}/authorities", method = RequestMethod.GET)
    public String toAuthoritiesPage(Map<String, Object> model, @PathVariable("clientAppId") int clientAppId) {
        model.put("clientAppId", clientAppId);
        if (clientAppId > 0) {
            List<Authority> authorities = authService.getAuthoritiesByClientAppId(clientAppId);
            model.put("authorities", authorities);
        }
        return super.getMyView("authorities.ftl");
    }

    // 授权资源列表数据
    @RequestMapping(value = "/{clientAppId}/authorities/edit", method = RequestMethod.POST)
    @ResponseBody
    public String editClientAppAuthorities(Map<String, Object> model, @PathVariable("clientAppId") int clientAppId,
            @RequestParam("resourceIds") String authIds) throws Exception {
        try {
            if (StringUtils.isEmpty(authIds)) {
                return JsonHelper.getSuccessJsonString("没有任何变动提交！");
            }
            service.saveAppClientAuthorities(clientAppId, authIds);
            return JsonHelper.getSuccessJsonString("保存成功！");
        } catch (Exception e) {
            throw new Exception(e);
        }
    }
}
